home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / portablc.zip / SPLIT_F.C < prev    next >
Text File  |  1990-01-18  |  11KB  |  330 lines

  1. /**********************************************************************
  2. *
  3. *       split_f:  Program to split and recombine binary files
  4. *
  5. *       USAGE:
  6. *               split_f  S  Ni  Complete_file  Section_file_prefix
  7. *       or      split_f  S  Ki  Complete_file  Section_file_prefix
  8. *       or      split_f  C  Complete_file  Section_file_list
  9. *
  10. *       where
  11. *               S indicates "split"     (may be upper or lower case)
  12. *               C indicates "combine"   (may be upper or lower case)
  13. *               Ni means "split into i files of equal size"
  14. *               Ki means "split into files of size i Kilobytes"
  15. *               Complete_file is the file to be split or recombined
  16. *               Section_files are the component files after split or
  17. *                       to be recombined into the Complete_file
  18. *               Section_file_prefix is the 8-character filename portion
  19. *                       of the Section_files.  The extension will be
  20. *                       numbers generated by the program.
  21. *               Section_file_list is a sequence of Section_files, each
  22. *                       separated by a space.
  23. *
  24. ***********************************************************************
  25. *
  26. *       REVISION HISTORY:
  27. *
  28. *               July 7, 1986: Created (Charlie Beerman)
  29. *               Jan. 18, 1990: Updated to run in UNIX  (Martin Katz)
  30. *                      Note: chmod 04755 on the executable.
  31. *
  32. **********************************************************************/
  33.  
  34.  
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #define FBUFSIZ 1024
  38. #define MAXFILES 99
  39. #define ERR (-1)
  40.  
  41. FILE *infile, *outfile;
  42.  
  43. main(argc,argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.  
  48. /****** start of executable code ******/
  49.  
  50. /* Check for at least 1 argument */
  51.  
  52.         if (argc < 2)
  53.                 userr("",0,0);  /* Print usage & exit */
  54.  
  55. /* Read first argument to decide action */
  56.         switch (tolower(argv[1][0]))
  57.         {
  58.                 case 'c':  /* Combine */
  59.                         if (argc < 4)
  60.                                 userr("Too few arguments",0,0);
  61.                         else
  62.                                 combine(argc,argv);
  63.                         break;
  64.                 case 's':  /* Split */
  65.                         if (argc < 5)
  66.                                 userr("Too few arguments",0,0);
  67.                         else
  68.                                 split(argc,argv);
  69.                         break;
  70.                 default:
  71.                         userr("Invalid first argument",0,0);
  72.                         break;  /* Code should never get to this line */
  73.         }
  74.  
  75. } /* end of main program */
  76.  
  77. /**********************************************************************
  78. *
  79. *       USERR:   Print error message and/or help info
  80. *
  81. *       INPUTS:
  82. *               errmsg  Text to output (if of nonzero length)
  83. *               file1   File to close (if nonzero)
  84. *               file2   File to close (if nonzero)
  85. *
  86. **********************************************************************/
  87.  
  88. userr(errmsg,file1,file2)
  89. char *errmsg;
  90. int file1, file2;
  91. {
  92.         int status;
  93.  
  94. /****** start of executable code ******/
  95.  
  96. /* close files if necessary */
  97.         if (file1 > 0)
  98.                 fclose(file1);
  99.         if (file2 > 0)
  100.                 fclose(file2);
  101.  
  102. /* print error message if there is one */
  103.         if (strlen(errmsg) > 0)
  104.                 printf("%s\n",errmsg);
  105.         else
  106.                 printf("%s%s%s%s",
  107. "Usage:\n",
  108. "       split_f  C  create_file_name  section_file_list\n",
  109. " or    split_f  S  Ni  source_file_name  section_file_prefix\n",
  110. " or    split_f  S  Ki  source_file_name  section_file_prefix\n");
  111.  
  112. /* now exit to DOS with errorlevel 1 */
  113.         exit(1);
  114.  
  115. } /* end USERR */
  116.  
  117.  
  118. /**********************************************************************
  119. *
  120. *       COMBINE:  Combine a list of files into one file
  121. *
  122. *       INPUTS:
  123. *               argc    Count of command-line arguments
  124. *               argv    List of command-line arguments
  125. *
  126. *       OUTPUTS:
  127. *               none
  128. *
  129. **********************************************************************/
  130.  
  131. combine(argc,argv)
  132. int argc;
  133. char *argv[];
  134. {
  135.         int i;
  136.         int nread, nwrote;
  137.         long destsiz;
  138.         char buf[FBUFSIZ];
  139.  
  140. /****** start of executable code ******/
  141.  
  142. /* open file to recombine */
  143.         printf("Creating file %s\n",argv[2]);
  144.         if ((outfile = fopen( argv[2], "w+b" )) == NULL )
  145.                 userr("Error in opening output file",0,0);
  146.  
  147.         destsiz = 0l;
  148.  
  149. /* loop through section files */
  150.         for (i = 3; i < argc; ++i)
  151.         {
  152.  
  153. /* open source file */
  154.                 printf("Reading from file %s\n",argv[i]);
  155.                 if ((infile = fopen( argv[i], "r+b" )) == NULL )
  156.                         userr("Error in opening input file",outfile,0);
  157.  
  158. /* loop through source file, reading a buffer at a time
  159.    into the destination file */
  160.         while ( (nread = fread(buf,1,FBUFSIZ,infile)) != 0 )
  161.         {
  162.                 nwrote = fwrite (buf, 1, nread, outfile);
  163.                 destsiz += nwrote;
  164.         }
  165.  
  166.  
  167. /* now close source file */
  168.                 fclose(infile);
  169.  
  170.         } /* end of loop over files in argument list */
  171.  
  172. /* now close destination file */
  173.                 fclose(outfile);
  174.  
  175.         printf("Total bytes written to %s: %ld.\n",argv[2], destsiz);
  176.  
  177. } /* end of COMBINE */
  178.  
  179. /**********************************************************************
  180. *
  181. *       SPLIT:  Split a file into components
  182. *
  183. *       INPUTS:
  184. *               argc    count of arguments on command line
  185. *               argv    text of arguments on command line
  186. *
  187. **********************************************************************/
  188.  
  189. split(argc,argv)
  190. int argc;
  191. char *argv[];
  192. {
  193.         int i, nfiles;
  194.         long maxsiz, srcsiz, destsiz, pos;
  195.         int nread, nwrote, nbytes;
  196.         char buf[FBUFSIZ], *p;
  197.         char prefix[61], secname[65], numstr[3];
  198.  
  199. /****** start of executable code ******/
  200.  
  201. /* figure out number of files to write and their maximum size */
  202.         p = argv[2];
  203.         switch (tolower(*p))
  204.         {
  205.                 case 'n':  /* user entered number of files */
  206.                         maxsiz = -1L;
  207.                         if ((nfiles = atoi(++p)) <= 0 ||
  208.                              nfiles > MAXFILES)
  209.                                 userr("Invalid number of files",0,0);
  210.                         break;
  211.  
  212.                 case 'k':  /* user entered maximum filesize */
  213.                         nfiles = -1;
  214.                         maxsiz = ((long) atoi(++p)) * 1024L;
  215.                         if (maxsiz < (long) (FBUFSIZ))
  216.                                 userr("Maximum size too small",0,0);
  217.                         break;
  218.  
  219.                 default:
  220.                         userr("Invalid second argument",0,0);
  221.                         break;
  222.         } /* end of switch */
  223.  
  224. /* open source file and find its size */
  225.         if ((infile = fopen( argv[3], "r+b" )) == NULL )
  226.                 userr("Error in opening source file",0,0);
  227.         else
  228.                 fseek(infile,0L,2);
  229.  
  230.         if ((srcsiz = ftell(infile)) <= (long) (FBUFSIZ))
  231.         {
  232.                 printf("Source size is %ld.\n", srcsiz);
  233.                 userr("Source file smaller than minimum split",infile,0);
  234.         }
  235.         else
  236.         {
  237.                 printf("Splitting file %s of size %ld\n",argv[3],srcsiz);
  238.                 rewind(infile);         /* rewind file */
  239.         }
  240.  
  241. /* finish doing filesize / # files calculation */
  242.         if (nfiles == -1)
  243.         {
  244.                 nfiles = (int) (srcsiz / maxsiz +1);
  245.                 if (nfiles > MAXFILES)
  246.                         userr("Too many section files",infile,0);
  247.         }
  248.         else if (maxsiz == -1L)
  249.         {
  250.                 maxsiz = srcsiz / nfiles + 1;
  251.                 if (maxsiz < (long) (FBUFSIZ))
  252.                         userr("Section file size too small",infile,0);
  253.         }
  254.         printf("Number of section files: %2d\n",nfiles);
  255.         printf("Maximum section file size:  %ld\n",maxsiz);
  256.  
  257. /* get section file name prefix and add ".-" identifier */
  258.         if (strlen(argv[4]) <= 0 || strlen(argv[4]) > 60)
  259.                 userr("Section file prefix too long",infile,0);
  260.         else
  261.         {
  262.                 strcpy(prefix,argv[4]);
  263.                 strcat(prefix,".-");
  264.         }
  265.  
  266. /* now loop over the number of files desired */
  267.         pos = 0L;
  268.         for (i = 1; i <= nfiles; ++i)
  269.         {
  270.  
  271. /* create filename */
  272.                 strcpy(secname,prefix);
  273.                 nwrote = sprintf(numstr,"%02d",i);
  274.                 strcat(secname,numstr);
  275.  
  276. /* initialize for this file */
  277.                 destsiz = 0L;
  278.  
  279. /* open file */
  280.                 printf("Creating section file %s:  ",secname);
  281.                 if ((outfile = fopen( secname, "w+b" )) == NULL )
  282.                         userr("Error in opening section file",infile,0);
  283.  
  284. /* read buffers from source file into section file
  285.    until maximum section file size is reached */
  286.                 nbytes = FBUFSIZ;
  287.                 nread = FBUFSIZ;
  288.                 while (nread == FBUFSIZ)
  289.                 {
  290.  
  291. /* are we within a bufferful of the maximum filesize? */
  292.                         if ((destsiz + (long) nbytes) > maxsiz)
  293.                                 nbytes = (int) (maxsiz - destsiz);
  294.  
  295. /* read from source */
  296.                         if (nbytes > 0)
  297.                                 nread = fread(buf,1,nbytes,infile);
  298.                         else
  299.                                 nread = 0;
  300.                         if (nread == ERR)
  301.                                 userr("Error in reading source file",
  302.                                        infile,outfile);
  303.  
  304. /* write to destination */
  305.                         if (nread > 0)
  306.                         {
  307.                                 nwrote = fwrite (buf, 1, nread, outfile);
  308.                                 destsiz += nwrote;
  309.                         }
  310.                         else
  311.                                 nwrote = 0;
  312.                         if (nwrote == ERR)
  313.                                 userr("Error in writing section file",
  314.                                        infile,outfile);
  315.  
  316.                 } /* end while */
  317.  
  318. /* close this destination file */
  319.                 pos += destsiz;
  320.                 fclose(outfile);
  321.                 printf(" %ld bytes\n",destsiz);
  322.  
  323.         } /* end for loop */
  324.  
  325. /* close source file */
  326.                 fclose(infile);
  327.                 printf("Total length:  %ld bytes\n",pos);
  328.  
  329. } /* end SPLIT */
  330.